package kr.co.skplanet.commute.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "point")
public class Point {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO) // 자동증가.
	private long id;
	
	@Column(name = "name", length=40)
	private String name;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "route_id")
	@JsonIgnore
	private Route route;
	
	@OneToMany(mappedBy = "point", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
	@OrderBy("id ASC")
	private List<StopInfo> stopinfos;
	
	public Point() {
		
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Route getRoute() {
		return route;
	}

	public void setRoute(Route route) {
		this.route = route;
	}
	
	public Long getRouteId() {
		return route.getId();
	}

	public List<StopInfo> getStopinfos() {
		return stopinfos;
	}

	public void setStopinfos(List<StopInfo> stopinfos) {
		this.stopinfos = stopinfos;
	}
	
	public void addStopInfo(StopInfo stopInfo) {
		if(stopinfos == null) {
			stopinfos = new ArrayList<StopInfo>();
		}
		stopinfos.add(stopInfo);
	}
	
	@JsonIgnore
	public Long getMemberId() {
		return route.getMemberId();
	}

	@Override
	public String toString() {
		return "Point [id=" + id + ", name=" + name + ", route=" + route
				+ ", stopinfos=" + stopinfos + "]";
	}
}
